package com.inossem.warehousestorage_inossem.adapter.receive;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveDeliveryOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveManageActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveManageBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class DeliveryListAdapter extends BaseAdapter {

    private static final String CODE_NOT_DONE = "0";

    private Context mContext;
    private List<ReceiveManageBean.DataBean.DeliveryListBean> mDeliveryList;
    private LayoutInflater inflater;
    private ReceiveManageActivity.ReceiveManageListener mReceiveManageListener;
    private String mDeliveryOrderNumber;

    public DeliveryListAdapter(Context context, List<ReceiveManageBean.DataBean.DeliveryListBean> deliveryList, String deliveryOrderNumber, ReceiveManageActivity.ReceiveManageListener receiveManageListener) {
        mContext = context;
        mDeliveryList = deliveryList;
        mReceiveManageListener = receiveManageListener;
        inflater = LayoutInflater.from(context);
        mDeliveryOrderNumber = deliveryOrderNumber;
    }

    @Override
    public int getCount() {
        return mDeliveryList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mDeliveryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_delivery_list_first_item, null);
            EditText deliveryOrderNumber = (EditText) convertView.findViewById(R.id.deliveryOrderNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            deliveryOrderNumber.setText(mDeliveryOrderNumber);
            deliveryOrderNumber.setHint(R.string.delivery_list_adapter_hint);
            deliveryOrderNumber.addTextChangedListener(new QRCodeEditTextWatcher(deliveryOrderNumber) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, deliveryOrderNumber);
                    mReceiveManageListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mReceiveManageListener.onNoDoubleClickListener(deliveryOrderNumber.getText().toString());
                }
            });
        } else {
            ReceiveManageBean.DataBean.DeliveryListBean deliveryItem = mDeliveryList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_delivery_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, deliveryItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.status = (TextView) view.findViewById(R.id.status);
        viewHolder.deliveryNumber = (TextView) view.findViewById(R.id.deliveryNumber);
        viewHolder.supplier = (TextView) view.findViewById(R.id.supplier);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ReceiveManageBean.DataBean.DeliveryListBean deliveryItem) {
        String statusMessage = "";
        if (CODE_NOT_DONE.equals(deliveryItem.getStatus())) {
            statusMessage += context.getResources().getString(R.string.activity_receive_manage_not_done);
            viewHolder.status.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_manage_not_receive));
        }
        viewHolder.status.setText(statusMessage);
        viewHolder.deliveryNumber.setText(deliveryItem.getDeliveryNumber());
        viewHolder.supplier.setText(deliveryItem.getSupplier());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, ReceiveDeliveryOverviewActivity.class);
                intent.putExtra(ReceiveExtra.DELIVERY_NUMBER, deliveryItem.getDeliveryNumber());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView status, deliveryNumber, supplier;
    }
}
